﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ElingOA6_Admin_DSJCJ.DAL
{
   /// <summary>
    /// 附件表 数据类
    /// </summary>
    public class DocAttach
    {
        public DocAttach()
        {

        }
        /// <summary>
        /// 获取相关信息的附件列表
        /// <creator>wangl</creator>
        /// <date>2013-08-14</date>
        /// </summary>
        /// <param name="docID">信息编号</param>
        /// <param name="attachType">附件类型</param>
        /// <returns></returns>
        public DataSet GetDocAttachListByDocID(decimal docID, string attachType)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FAttachID,FAttachDesc,FAttachSvrFileName,FAttachType,FAttachSource,FAttachFileExt,FAttachOldFileName ");
            strSql.Append(" FROM t_DocAttach ");
            strSql.Append(" WHERE FAttachSource=@FAttachSource AND FAttachType=@FAttachType");
            SqlParameter[] parameters = {
					new SqlParameter("@FAttachSource", SqlDbType.Decimal,18),
                    new SqlParameter("@FAttachType", SqlDbType.NVarChar,80)
                                           };
            parameters[0].Value = docID;
            parameters[1].Value = attachType;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            return ds;
        }
        /// <summary>
        /// 获取附件服务器地址
        /// <creator>wangl</creator>
        /// <date>2013-08-14</date>
        /// </summary>
        /// <param name="attachID">附件编号</param>
        /// <returns></returns>
        public string GetAttachSvrAddress(decimal attachID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT FAttachSvrFileName FROM t_DocAttach WHERE FAttachID=@FAttachID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAttachID", SqlDbType.Decimal,18)
                                           };
            parameters[0].Value = attachID;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return obj.ToString();
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 删除附件记录
        /// <creator>wangl</creator>
        /// <date>2013-08-14</date>
        /// </summary>
        /// <param name="attachID">附件编号</param>
        public void DeleteDocAttach(decimal attachID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM t_DocAttach WHERE FAttachID=@FAttachID");
            SqlParameter[] parameters = {
					new SqlParameter("@FAttachID", SqlDbType.Decimal,18)
                                           };
            parameters[0].Value = attachID;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

        }
        /// <summary>
        /// 得到一个对象实体
        /// <creator>wangl</creator>
        /// <date>2013-08-14</date> 
        /// </summary>
        /// <param name="row">数据行</param>
        /// <returns>信息类别实体</returns>
        public ElingOA6_Admin_DSJCJ.Model.DocAttach DataRowToModel(DataRow row)
        {
             ElingOA6_Admin_DSJCJ.Model.DocAttach model = new ElingOA6_Admin_DSJCJ.Model.DocAttach();
             if (row != null)
             {
                 if (row["FAttachID"] != null && row["FAttachID"].ToString()!="")
                 {
                     model.FAttachID = decimal.Parse(row["FAttachID"].ToString());
                 }
                 if (row["FAttachDesc"] != null )
                 {
                     model.FAttachDesc = row["FAttachDesc"].ToString();
                 }
                 if (row["FAttachSvrFileName"] != null)
                 {
                     model.FAttachSvrFileName = row["FAttachSvrFileName"].ToString();
                 }
                 if (row["FAttachType"] != null)
                 {
                     model.FAttachType = row["FAttachType"].ToString();
                 }
                 if (row["FAttachSource"] != null && row["FAttachSource"].ToString() != "")
                 {
                     model.FAttachSource = decimal.Parse(row["FAttachSource"].ToString());
                 }
                 if (row["FAttachFileExt"] != null)
                 {
                     model.FAttachFileExt = row["FAttachFileExt"].ToString();
                 }
                 if (row["FAttachOldFileName"] != null)
                 {
                     model.FAttachOldFileName = row["FAttachOldFileName"].ToString();
                 }
             }
             return model;
        }
        public DataSet GetSvrFileNameAndOldName(decimal attachID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 FAttachSvrFileName,FAttachOldFileName FROM t_docAttach");
            strSql.Append(" where FAttachID=@FAttachID");

            SqlParameter[] parameters = {			
					new SqlParameter("@FAttachID", SqlDbType.Decimal)};

            parameters[0].Value = attachID;

            return DbHelperSQL.Query(strSql.ToString(), parameters);

        }
    }
}
